package com.cgbl.cg.ui.mine

import android.os.Bundle
import android.view.View
import androidx.recyclerview.widget.StaggeredGridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.adapter.WorksCollectionAdapter
import com.cgbl.cg.adapter.WorksCollectionAdapter2
import com.cgbl.cg.databinding.AppActivityPersonalPageBinding
import com.cgbl.cg.view.CommonConfirmPop
import com.cgbl.cg.view.CreateFolderPop
import com.cgbl.cg.vm.mine.PersonalPageViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CollectDetailBean
import com.robot.base.model.WorksBean
import com.robot.base.util.arouter.ARouterUtil
import com.scwang.smartrefresh.layout.api.RefreshLayout
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener


@Route(path = AppConstants.Router.Mine.A_MINE_PERSONAL_PAGE)
class PersonalPageActivity :
    GLBaseActivity<AppActivityPersonalPageBinding, PersonalPageViewModel>() {

    lateinit var worksAdapter: WorksCollectionAdapter

    lateinit var collectionAdapter: WorksCollectionAdapter2

    private lateinit var deletePop: CommonConfirmPop

    private lateinit var editPop: CommonConfirmPop

    private lateinit var createFolderPop: CreateFolderPop

    override fun initComponents() {
        initTiterBar()
        initPop()
        initView()
        viewModel.refresh()
        viewModel.fetchWorkDraft()
        viewModel.collectPage()
    }

    private fun initView() {
        worksAdapter = WorksCollectionAdapter()
        collectionAdapter = WorksCollectionAdapter2()
        binding.recyclerView.apply {
            layoutManager = StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL)
            adapter = worksAdapter
        }

        binding.refreshLayout.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.refresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.loadMore()
            }
        })
        collectionAdapter.setOnItemClickListener { adapter, view, position ->
            val item = collectionAdapter.data[position]
            ARouterUtil.goToActivityWithBundle(AppConstants.Router.COLLECTION.A_COLLECTIONS_DETAILS,
                Bundle().apply {
                    putString(
                        AppConstants.BundleKey.COLLECTION_DATA,
                        item.collectId
                    )
                }
            )
        }
    }

    private fun initTiterBar() {
    }

    private fun initPop() {
        deletePop = CommonConfirmPop(this)
        editPop = CommonConfirmPop(this)
        createFolderPop = CreateFolderPop(this)
        createFolderPop.setOnCreateFolderListener { name, desc, isOpen ->
            viewModel.createCollect(name, desc, isOpen)
        }
    }

    override fun initViewObservable() {
        viewModel.uc.showDeleteRoughEvent.observe(this) {
            XPopup.Builder(this).asCustom(deletePop).show()
            deletePop.setTitle("确认删除该草稿箱吗?").setLeftText("取消").setRightText("删除")
                .setRightClickListener {

                }
        }
        viewModel.uc.showEditRoughEvent.observe(this) {
            XPopup.Builder(this).asCustom(editPop).show()
            editPop.setTitle("有草稿未发布，是否继续编辑？").setLeftText("新建作品").setRightText("继续编辑")
                .setRightClickListener {
                    ARouterUtil.goToActivityWithBundle(
                        AppConstants.Router.WORK.A_RELEASE_WORK,
                        Bundle().apply {
                            putSerializable(
                                AppConstants.BundleKey.RELEASE_DRAFT_WORKS, viewModel.draftWorks
                            )
                        })
                }.setLeftClickListener {
                    ARouterUtil.goToActivity(AppConstants.Router.WORK.A_RELEASE_WORK)
                }
        }

        viewModel.uc.showCreateFolderEvent.observe(this) {
            XPopup.Builder(this).moveUpToKeyboard(false).asCustom(createFolderPop).show()
        }
        viewModel.uc.newWorksData.observe(this) {
            if (it.isEmpty()) {
                val emptyView = layoutInflater.inflate(R.layout.app_layout_empty, null)
                worksAdapter.setEmptyView(emptyView)
            }
            worksAdapter.setNewInstance(it as MutableList<WorksBean>?)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        viewModel.uc.loadMoreWorksData.observe(this) {
            worksAdapter.addData(it)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        viewModel.uc.loadWorkDraftEvent.observe(this) {
            binding.roughLayout.visibility = if (it == null) View.GONE else View.VISIBLE
        }

        viewModel.uc.newCollectionPageData.observe(this) {
            if (it.isEmpty()) {
                val emptyView = layoutInflater.inflate(R.layout.app_layout_empty, null)
                collectionAdapter.setEmptyView(emptyView)
            }
            collectionAdapter.setNewInstance(it as MutableList<CollectDetailBean>?)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }
        viewModel.uc.loadMoreCollectionPageData.observe(this) {
            collectionAdapter.addData(it)
            binding.refreshLayout.finishLoadMore()
            binding.refreshLayout.finishRefresh()
        }

        viewModel.uc.switchTabEvent.observe(this) {
            binding.recyclerView.adapter = if (it) worksAdapter else collectionAdapter
        }
    }

    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_personal_page

    override fun initVariableId(): Int = BR.viewModel

}